// 获取类型
import type {
  IState,
  IStore
} from '../types/type'

import { inject } from 'vue'

import type BaseArray from '../base2/base-array'
import type BaseObject from '../base2/base-object'

// 状态的容器
import { store, _storeFlag } from './store'

/**
 * 获取全局状态。implements IState
 * @param id 全局状态 的 ID，string | symbol
 * @returns 指定的全局的状态
 */
export default function useStore<T extends object> (id: string | symbol) {
  // const re = inject(_storeFlag) ?? store
  const re: IStore = inject<IStore>(_storeFlag) ?? store

  if ((re[id] as IState).$isObject) {
    return reBaseObject<T>(re[id])
  } else if ((re[id] as IState).$isArray) {
    return reBaseArray<T>(re[id])
  } else if (re[id]) {
    return re[id] as T
  }

  console.error('没有找到这个状态：', id)
  return {}
}


export function reBaseObject<T extends object> (state: any) {
  return state as T & BaseObject<T>
}

export function reBaseArray<T> (state: any) {
  return state as T & BaseArray<T>
}